package com.twoandone.tms.realtime.publisher.controller;

import com.google.common.base.Strings;
import com.twoandone.tms.realtime.publisher.bean.TransportCity;
import com.twoandone.tms.realtime.publisher.service.TransportService;
import com.twoandone.tms.realtime.publisher.util.DateFormatUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * @packageName:com.twoandone.tms.realtime.controller
 * @ClassName:TransportController
 * @Description:
 * @author:
 * @date 2023/3/12 22:38
 **/
@RestController
@RequestMapping("/transport")
public class TransportController {
    @Autowired
    TransportService transportService;

//    @GetMapping("/total/count")
    @RequestMapping("/total/count")
    public String getTotalCount(@RequestParam(value = "date",defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        Long totalCount = transportService.getTotalCount(date);
        String result = "{\"status\": 0,\"data\": " + totalCount + "}";
        return result;
    }

    /**
     * 省份运输次数
     * @param date
     * @return
     */
    @RequestMapping("/city/count")
    public String getProvinceCount(@RequestParam(value = "date",defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        Long totalCount = transportService.getProvinceCount(date);
        String result = "{\"status\": 0,\"data\": " + totalCount + "}";
        return result;
    }

    /**
     * 城市运输里程
     * @param date
     * @return
     */
    @RequestMapping("/city/distance")
    public String getCityDistance(@RequestParam(value = "date",defaultValue = "0") Integer date,
                                  @RequestParam(value = "limit",defaultValue = "0") Integer limit) {
        if (date == 0) {
            date = DateFormatUtil.now();
//            System.out.println(date);、
        }
        if (limit == 0) {
            limit = 5;
//            System.out.println(date);、
        }
        List<TransportCity> totalCount = transportService.getCityDistance(date,limit);
//        System.out.println("totalCount = " + totalCount);
        ArrayList<String> cityName = new ArrayList<>();
        ArrayList<BigDecimal> sumDistance = new ArrayList<>();
        for (TransportCity transportCity : totalCount) {
            cityName.add(transportCity.getCityName());
            sumDistance.add(transportCity.getSumDistance());
        }
        String result="{\"status\": 0,\"data\": {\"categories\": [\""+ StringUtils.join(cityName,"\",\"") +"\"],\"" +
                "series\": [{\"name\": \"运输里程\",\"data\": ["+ StringUtils.join(sumDistance,",") +"]}]}}";
//        String result = "{\"status\": 0,\"data\": {\"categories\":[\""+ StringUtils.join(cityName,"\",\"") + "\"],"
//                +"\"series\": [{\"name\": \"运输里程\",\"data\": ["+StringUtils.join(sumDistance,",")+"}]}}";
        return result;
    }
}
